package com.example.another.bottomnavigationdemo1;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.example.another.bottomnavigationdemo1.fragment.FirstFragment;
import com.example.another.bottomnavigationdemo1.fragment.FiveFragment;
import com.example.another.bottomnavigationdemo1.fragment.FourFragment;
import com.example.another.bottomnavigationdemo1.fragment.SecondFragment;
import com.example.another.bottomnavigationdemo1.fragment.ThirdFragment;

/***
 * 参考博客：https://blog.csdn.net/jxq1994/article/details/52573506
 */
public class FirstActivity extends AppCompatActivity implements View.OnClickListener {

    private Fragment mFragment1, mFragment2, mFragment3, mFragment4, mFragment5;
    private TextView mTextView1, mTextView2, mTextView3, mTextView4, mTextView5;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_first);

        initViews();
    }

    private void initViews() {
        mTextView1 = (TextView) findViewById(R.id.menu1);
        mTextView2 = (TextView) findViewById(R.id.menu2);
        mTextView3 = (TextView) findViewById(R.id.menu3);
        mTextView4 = (TextView) findViewById(R.id.menu4);
        mTextView5 = (TextView) findViewById(R.id.menu5);

        mTextView1.setOnClickListener(this);
        mTextView2.setOnClickListener(this);
        mTextView3.setOnClickListener(this);
        mTextView4.setOnClickListener(this);
        mTextView5.setOnClickListener(this);

        FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
        mTextView1.setSelected(true);
        if (mFragment1 == null) {
            mFragment1 = new FirstFragment();
            fragmentTransaction.add(R.id.fragment_container, mFragment1).commit();
        } else {
            fragmentTransaction.show(mFragment1).commit();
        }

    }

    private void initSelected() {
        mTextView1.setSelected(false);
        mTextView2.setSelected(false);
        mTextView3.setSelected(false);
        mTextView4.setSelected(false);
        mTextView5.setSelected(false);
    }

    private void hiddFragments(FragmentTransaction fragmentTransaction) {

        if (mFragment1 != null) {
            fragmentTransaction.hide(mFragment1);
        }
        if (mFragment2 != null) {
            fragmentTransaction.hide(mFragment2);
        }

        if (mFragment3 != null) {
            fragmentTransaction.hide(mFragment3);
        }

        if (mFragment4 != null) {
            fragmentTransaction.hide(mFragment4);
        }

        if (mFragment5 != null) {
            fragmentTransaction.hide(mFragment5);
        }
    }

    @Override
    public void onClick(View v) {
        initSelected();
        FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
        hiddFragments(fragmentTransaction);

        switch (v.getId()) {
            case R.id.menu1:
                mTextView1.setSelected(true);
                if (mFragment1 == null) {
                    mFragment1 = new FirstFragment();
                    fragmentTransaction.add(R.id.fragment_container, mFragment1);
                } else {
                    fragmentTransaction.show(mFragment1);
                }
                break;
            case R.id.menu2:
                mTextView2.setSelected(true);
                if (mFragment2 == null) {
                    mFragment2 = new SecondFragment();
                    fragmentTransaction.add(R.id.fragment_container, mFragment2);
                } else {
                    fragmentTransaction.show(mFragment2);
                }
                break;

            case R.id.menu3:
                mTextView3.setSelected(true);
                if (mFragment3 == null) {
                    mFragment3 = new ThirdFragment();
                    fragmentTransaction.add(R.id.fragment_container, mFragment3);
                } else {
                    fragmentTransaction.show(mFragment3);
                }
                break;

            case R.id.menu4:
                mTextView4.setSelected(true);
                if (mFragment4 == null) {
                    mFragment4 = new FourFragment();
                    fragmentTransaction.add(R.id.fragment_container, mFragment4);
                } else {
                    fragmentTransaction.show(mFragment4);
                }
                break;

            case R.id.menu5:
                mTextView5.setSelected(true);
                if (mFragment5 == null) {
                    mFragment5 = new FiveFragment();
                    fragmentTransaction.add(R.id.fragment_container, mFragment5);
                } else {
                    fragmentTransaction.show(mFragment5);
                }
                break;
            default:
                break;
        }
        fragmentTransaction.commit();
    }
}
